<?php

namespace App\Listeners\CommentCreated;

use App\Events\CommentCreated;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class UpdateUserCommentCount
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  CommentCreated  $event
     * @return void
     */
    public function handle(CommentCreated $event)
    {
        $comment = $event->comment;
        $user = $comment->user;
        $commentCount = $user->comments()->withTrashed()->count();
        $topicCount = $user->topics()->withTrashed()->count();

        $user->comment_count = $commentCount;
        $user->save();

        logger(sprintf(
            "%s, user: %s, commentCount: %s, topicCount: %s",
            __METHOD__, $user->id, $commentCount, $topicCount
        ));

    }
}
